package com.atguigu.srb.core.service.impl;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.exception.BusinessException;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.util.MD5;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserLoginRecordMapper;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserLoginRecord;
import com.atguigu.srb.core.pojo.query.UserInfoQuery;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author Dragon
 * @since 2021-07-29
 */


@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {
    @Resource
    private UserLoginRecordMapper userLoginRecordMapper;

    @Resource
    UserAccountMapper userAccountMapper;

    @Resource
     private RedisTemplate redisTemplate;

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void register(RegisterVO registerVO) {

        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String code = registerVO.getCode();

        Assert.notNull(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notNull(password,ResponseEnum.PASSWORD_NULL_ERROR);
        String redisCode = (String) redisTemplate.opsForValue().get("srb:sms:code:13543334815");
        Assert.equals(code,redisCode,ResponseEnum.CODE_ERROR);

        QueryWrapper<UserInfo> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq("mobile",mobile);
        Integer count =  baseMapper.selectCount(QueryWrapper);
        Assert.isTrue(count == 0, ResponseEnum.MOBILE_EXIST_ERROR);

        //插入用户基本信息
        UserInfo userInfo = new UserInfo();
        userInfo.setUserType(registerVO.getUserType());
        userInfo.setNickName(registerVO.getMobile());
        userInfo.setName(registerVO.getMobile());
        userInfo.setMobile(registerVO.getMobile());
        userInfo.setPassword(MD5.encrypt(registerVO.getPassword()));
        userInfo.setStatus(UserInfo.STATUS_NORMAL); //正常
        //设置一张静态资源服务器上的头像图片
        userInfo.setHeadImg("https://srb-file.oss-cn-beijing.aliyuncs.com/avatar/07.jpg");
        baseMapper.insert(userInfo);

        //创建会员账户
        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userInfo.getId());
        userAccountMapper.insert(userAccount);
    }



    @Override
    public UserInfoVO login(LoginVO loginVO, String ip) {
        String password = loginVO.getPassword();
        String mobile = loginVO.getMobile();
        Integer userType = loginVO.getUserType();
        QueryWrapper<UserInfo> QueryWrapper = new QueryWrapper<>();
      /*   QueryWrapper.eq("password",password);*/
         QueryWrapper.eq("mobile",mobile);
        UserInfo userInfo = baseMapper.selectOne(QueryWrapper);
         Assert.notNull(userInfo,ResponseEnum.USER_NOT_EXIST);
        Assert.equals(MD5.encrypt(password),userInfo.getPassword(), ResponseEnum.LOGIN_PASSWORD_ERROR);

        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setName("龙哥");
        userInfoVO.setUserType(userType);
        userInfoVO.setNickName("哥");
        userInfoVO.setMobile(mobile);


        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());
        userInfoVO.setToken(token);
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setUserId(userInfo.getId());
        userLoginRecord.setIp(ip);
        userLoginRecordMapper.insert(userLoginRecord);
         return  userInfoVO;
    }

    @Override
    public IPage<UserInfo> listpage(Page<UserInfo> pageOne, UserInfoQuery userInfoQuery) {
        if (userInfoQuery==null) {
            return baseMapper.selectPage(pageOne,null);
        }

        String mobile = userInfoQuery.getMobile();
        Integer userType = userInfoQuery.getUserType();
        Integer status = userInfoQuery.getStatus();

        QueryWrapper<UserInfo> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq(StringUtils.isNotBlank(mobile),"mobile",mobile).eq(
                status!=null,"status",status).eq(userType!=null,"user_type",userType);
        return baseMapper.selectPage(pageOne,QueryWrapper);

    }

    @Override
    public void lock(Long id, Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setStatus(status);
        baseMapper.updateById(userInfo);
    }

    @Override
    public Boolean checkMobile(String mobile) {

        QueryWrapper<UserInfo> QueryWrapper = new QueryWrapper<>();
         QueryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(QueryWrapper);
        return count>0;
    }


}
